var location = require("../../mybehaviors/location/location");
var app = getApp();
Page({
  behaviors: [location],
  data: {
    page: 1,
    pageSize: 20,
    footshow: true,
    foot_type: '',
    currentLocation: '',
    searchKeyword: '',
    activeFilter: 'recommend',
    members: [],
    isLoading: false,
    isLoadMore: true,
    total: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 如果有参数传递，可以从options中获取
    this._getLocation((res) => {
      this.setData({
        currentLocation: res.address_component.city ? res.address_component.city : res.address_component.province,
        longitude: res.ad_info.location.lat,
        latitude: res.ad_info.location.lng
      });
      this.getData();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 下拉刷新逻辑
    wx.showNavigationBarLoading();
    this.setData({
      page: 1,
      isLoadMore: true,
      members: []
    });
    this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 上拉加载更多
    if (this.data.isLoadMore && !this.data.isLoading) {
      this.setData({
        page: this.data.page + 1
      });
      this.getData();
    } else if (!this.data.isLoadMore) {
      wx.showToast({
        icon: 'none',
        title: '没有更多数据了',
        duration: 1000
      });
    }
  },

  getData() {
    let that = this;
    // 显示加载状态
    if (this.data.page === 1) {
      wx.showLoading({
        title: '加载中...',
      });
    }
    this.setData({
      isLoading: true
    });
    
    let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-member.business-card-member-list");
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
        pageSize: this.data.pageSize,
        search: this.data.searchKeyword,
        longitude: this.data.longitude,
        latitude:this.data.latitude
      },
      success: function(resdata) {
        console.log(resdata);
        var res = resdata.data;
        if (res.result == 1) {
          const newData = res.data.list || [];
          const total = res.data.total || 0;
          const currentList = that.data.page === 1 ? newData : [...that.data.members, ...newData];
          
          that.setData({
            members: currentList,
            total: total,
            isLoadMore: currentList.length < total
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
        wx.showToast({
          icon: 'none',
          title: '网络请求失败',
          duration: 1500
        });
      },
      complete: function() {
        // 隐藏加载状态
        that.setData({
          isLoading: false
        });
        
        if (that.data.page === 1) {
          wx.hideLoading();
        }
        // 停止下拉刷新动画
        wx.stopPullDownRefresh();
        // 隐藏导航栏加载动画
        wx.hideNavigationBarLoading();
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '找人',
      path: '/packageX/member_search/member_search'
    };
  },

  /**
   * 搜索输入
   */
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
    this.getData();
    // 可以在这里实现搜索逻辑
  },

  /**
   * 筛选点击
   */
  onFilterClick(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      activeFilter: type
    });
    // 这里可以根据筛选类型过滤会员列表
  },

  /**
   * 查看详情
   */
  onDetailClick(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/packageB/member/business_card/BusinessCard/BusinessCard?card_id=${id}`,
    });
  },

  /**
   * 显示名片
   */
  onShowCard() {
    // 这里可以实现显示名片的逻辑
    wx.showToast({
      title: '显示名片',
      icon: 'success',
      duration: 2000
    });
  },

  /**
   * 显示购物车底部导航
   */
  showcarrfoot() {
    // 实现显示购物车底部导航的逻辑
  },

  /**
   * 显示IPhone适配的购物车底部导航
   */
  showcarrfoot_ipx() {
    // 实现显示IPhone适配的购物车底部导航的逻辑
  }
});